﻿// HtaHelperXCtrl.cpp : CHtaHelperXCtrl ActiveX 控件类的实现。

#include "pch.h"
#include "framework.h"
#include "HtaHelperX.h"
#include "HtaHelperXCtrl.h"
#include "HtaHelperXPropPage.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNCREATE(CHtaHelperXCtrl, COleControl)

// 消息映射

BEGIN_MESSAGE_MAP(CHtaHelperXCtrl, COleControl)
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()

// 调度映射

BEGIN_DISPATCH_MAP(CHtaHelperXCtrl, COleControl)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Max", dispidMax, Max, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Min", dispidMin, Min, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Restore", dispidRestore, Restore, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Exit", dispidExit, Exit, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Move", dispidMove, Move, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Exec", dispidExec, Exec, VT_EMPTY, VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR VTS_UI2)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Find", dispidFind, Find, VT_I4, VTS_BSTR)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "Show", dispidShow, Show, VT_I4, VTS_BSTR VTS_BSTR VTS_I4)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "MoveW", dispidMoveW, MoveW, VT_EMPTY, VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_I4 VTS_I4)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "GetMainID", dispidGetMainID, GetMainID, VT_I4, VTS_NONE)
	DISP_FUNCTION_ID(CHtaHelperXCtrl, "SwitchW", dispidSwitchW, SwitchW, VT_EMPTY, VTS_I4)
END_DISPATCH_MAP()

// 事件映射

BEGIN_EVENT_MAP(CHtaHelperXCtrl, COleControl)
END_EVENT_MAP()

// 属性页

// TODO: 根据需要添加更多属性页。请记住增加计数!
BEGIN_PROPPAGEIDS(CHtaHelperXCtrl, 1)
	PROPPAGEID(CHtaHelperXPropPage::guid)
END_PROPPAGEIDS(CHtaHelperXCtrl)

// 初始化类工厂和 guid

IMPLEMENT_OLECREATE_EX(CHtaHelperXCtrl, "MFCACTIVEXCONTRO.HtaHelperXCtrl.1",
	0xeee5d0fa,0x03f5,0x4b50,0x8b,0xe6,0xc5,0x0b,0xed,0xdd,0x86,0xde)

// 键入库 ID 和版本

IMPLEMENT_OLETYPELIB(CHtaHelperXCtrl, _tlid, _wVerMajor, _wVerMinor)

// 接口 ID

const IID IID_DHtaHelperX = {0x0c6c2858,0x518e,0x47a0,{0xa2,0x84,0x62,0xc6,0xc8,0xf3,0xf3,0xa6}};
const IID IID_DHtaHelperXEvents = {0x28515000,0x56ac,0x4eab,{0xaa,0x13,0xa5,0x2b,0x2e,0xb7,0xcd,0x4e}};

// 控件类型信息

static const DWORD _dwHtaHelperXOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CHtaHelperXCtrl, IDS_HTAHELPERX, _dwHtaHelperXOleMisc)

// CHtaHelperXCtrl::CHtaHelperXCtrlFactory::UpdateRegistry -
// 添加或移除 CHtaHelperXCtrl 的系统注册表项

BOOL CHtaHelperXCtrl::CHtaHelperXCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO:  验证您的控件是否符合单元模型线程处理规则。
	// 有关更多信息，请参考 MFC 技术说明 64。
	// 如果您的控件不符合单元模型规则，则
	// 必须修改如下代码，将第六个参数从
	// afxRegApartmentThreading 改为 0。

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_HTAHELPERX,
			IDB_HTAHELPERX,
			afxRegApartmentThreading,
			_dwHtaHelperXOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


// CHtaHelperXCtrl::CHtaHelperXCtrl - 构造函数

CHtaHelperXCtrl::CHtaHelperXCtrl()
{
	InitializeIIDs(&IID_DHtaHelperX, &IID_DHtaHelperXEvents);
	// TODO:  在此初始化控件的实例数据。
	main_hwnd = ::AfxGetMainWnd()->m_hWnd;
}

// CHtaHelperXCtrl::~CHtaHelperXCtrl - 析构函数

CHtaHelperXCtrl::~CHtaHelperXCtrl()
{
	// TODO:  在此清理控件的实例数据。
}

// CHtaHelperXCtrl::OnDraw - 绘图函数

void CHtaHelperXCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& /* rcInvalid */)
{
	if (!pdc)
		return;

	// TODO:  用您自己的绘图代码替换下面的代码。
	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(GRAY_BRUSH)));
}

// CHtaHelperXCtrl::DoPropExchange - 持久性支持

void CHtaHelperXCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: 为每个持久的自定义属性调用 PX_ 函数。
}


// CHtaHelperXCtrl::OnResetState - 将控件重置为默认状态

void CHtaHelperXCtrl::OnResetState()
{
	COleControl::OnResetState();  // 重置 DoPropExchange 中找到的默认值

	// TODO:  在此重置任意其他控件状态。
}


// CHtaHelperXCtrl::AboutBox - 向用户显示“关于”框

void CHtaHelperXCtrl::AboutBox()
{
	CDialogEx dlgAbout(IDD_ABOUTBOX_HTAHELPERX);
	dlgAbout.DoModal();
}


// CHtaHelperXCtrl 消息处理程序


void CHtaHelperXCtrl::Max()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::SendMessage(main_hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}


void CHtaHelperXCtrl::Min()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::SendMessage(main_hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
}


void CHtaHelperXCtrl::Restore()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::SendMessage(main_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
}


void CHtaHelperXCtrl::Exit()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::PostQuitMessage(0);
}


void CHtaHelperXCtrl::Move()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::PostMessage(main_hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}


void CHtaHelperXCtrl::Exec(BSTR oper, BSTR path, BSTR params, BSTR dir, USHORT show)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	ShellExecute(main_hwnd, oper, path, params, dir, show);
}


LONG CHtaHelperXCtrl::Find(BSTR title)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	return (LONG)::FindWindow(NULL, title);
}


LONG CHtaHelperXCtrl::Show(BSTR text, BSTR title, LONG type)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码

	return MessageBox(text, title, type);
}


void CHtaHelperXCtrl::MoveW(LONG hwnd, LONG x, LONG y, LONG width, LONG height, LONG refresh)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::MoveWindow((HWND)hwnd, x, y, width, height, refresh);
}

LONG CHtaHelperXCtrl::GetMainID()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码

	return (LONG)main_hwnd;
}


void CHtaHelperXCtrl::SwitchW(LONG hwnd)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// TODO: 在此处添加分派处理程序代码
	::SwitchToThisWindow((HWND)hwnd, true);
}
